function fallbackCopyTextToClipboard(id) {
    const range = document.createRange();
    range.selectNode(document.getElementById(id));
    const selection = window.getSelection();
    if (selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    try {
        const successful = document.execCommand('copy'); //复制选中的文字到剪贴板
        selection.removeRange(range); // 移除选中的元素
        var msg = successful ? 'successful' : 'unsuccessful';
        // console.log
        return msg;
    } catch (err) {
        selection.removeRange(range);
        console.error('Fallback: Oops, unable to copy', err);
        return false;
    }
}

function copyTextToClipboard(id) {
    if (!navigator.clipboard) {
        return fallbackCopyTextToClipboard(id);
    }
    const range = document.createRange();
    range.selectNode(document.getElementById(id));
    const selection = window.getSelection();
    if (selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    return navigator.clipboard.writeText(selection).then(function () {
        selection.removeRange(range);
        // console.log
        return 'successful';
    }, function (err) {
        selection.removeRange(range);
        console.error('Async: Could not copy text: ', err);
        return false;
    });
}

export default {
    copyTextToClipboard,
}
